package com.solution.framework.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created with IntelliJ IDEA.
 * Busline: sunguangzhu
 * Date: 13-11-21
 * Time: 上午10:42
 * To change this template use File | Settings | File Templates.
 */
public class JsonUtil {
    public static String transformObjectToString(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String result = "";
        try {
            result = mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return result;
    }

    public static Object transformJsonToBean(String json, Class<?> clazz) {
        ObjectMapper mapper = new ObjectMapper();
        Object bean = null;
        try {
            bean = mapper.readValue(json, clazz);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return bean;
        //To change body of created methods use File | Settings | File Templates.
    }

}
